
#include "bsp_B_phase_tim.h"
#include "bsp_gpio_output.h"
#include "bsp_exti.h"

/**
  * @brief  定时器14中断优先级配置
  * @param  无
  * @retval 无
  */
static void PHASE_B_TIM_NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  // 设置中断来源
  NVIC_InitStructure.NVIC_IRQChannel = PHASE_B_TIM_IRQn;
  // 设置抢占优先级
  NVIC_InitStructure.NVIC_IRQChannelPriority =3;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

/**
  * @brief  定时器2模式配置
  * @param  无
  * @retval 无
  */
static void PHASE_B_TIM_Mode_Config(void)
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  //开启定时器2时钟
  RCC_APB1PeriphClockCmd(PHASE_B_TIM_CLK, ENABLE);
  TIM_TimeBaseStructure.TIM_Prescaler = 48 - 1;
  //定时器分频因子，配置死区时间时需要用到
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  //计数器计数模式，设置为向上计数
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  //重复计数器的值,没有用到不用管
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  // 初始化定时器TIMx, x[2,3,4,5]
  TIM_TimeBaseInit(PHASE_B_TIM, &TIM_TimeBaseStructure);

  // 清除定时器更新中断标志位
  TIM_ClearFlag(PHASE_B_TIM,TIM_FLAG_Update);

  // 开启定时器更新中断
  TIM_ITConfig(PHASE_B_TIM,TIM_IT_Update,ENABLE);

  // 使能定时器
  TIM_Cmd(PHASE_B_TIM, DISABLE);
}

/**
  * @brief  定时器2配置
  * @param  无
  * @retval 无
  */
void Phase_B_Tim_Configuration(void)
{
  PHASE_B_TIM_Mode_Config();
  PHASE_B_TIM_NVIC_Configuration();
}

void PHASE_B_TIM_IRQHandler(void)
{
  if(TIM_GetITStatus(PHASE_B_TIM, TIM_IT_Update) != RESET)
  {
    TIM_ClearITPendingBit(PHASE_B_TIM, TIM_IT_Update);
    TIM_Cmd(PHASE_B_TIM,DISABLE);
    B_PHASE_INDICATOR_TOGGLE;
    B_OUTPUT(0);
    Delay_us(80);
    B_OUTPUT(1);
  }
}
/*********************************************END OF FILE**********************/